//
//  TTOrderGoodsViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/10/8.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTOrderGoodsViewCell.h"
#import "TTOrderShopModel.h"
#import "TTUpDownButton.h"
#import <UIImageView+WebCache.h>

@interface TTOrderGoodsViewCell ()

@property (nonatomic, strong) TTOrderShopModel *model;

@property (nonatomic, strong) UIImageView *leftImageView;
///售罄图片
@property (nonatomic, strong) UIImageView *selloutImageView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) TTUpDownButton *locationBtn;
@property (nonatomic, strong) UILabel *bottomLabel; // 定制才有，做工：颜色：
@property (nonatomic, strong) UILabel *productionTimeLabel; // 生产周期
@property (nonatomic, strong) UILabel *priceLabel;  // 价格

@end

@implementation TTOrderGoodsViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTOrderShopModel *)model hasShadow:(BOOL)hasShadow isGray:(BOOL)isGray
{
    [self setModel:model hasShadow:hasShadow isGray:isGray isFromDianpu:NO];
}

- (void)setModel:(TTOrderShopModel *)model hasShadow:(BOOL)hasShadow isGray:(BOOL)isGray isFromDianpu:(BOOL)isFromDianpu
{
    _model = model;
    
    NSString *imageUrlString = [model.GoodsImage urlEncode];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    self.leftLabel.text = model.ProductText;
    
    
    self.titleLabel.text = model.GoodsTitle;
    NSData *data = [model.GoodsDescribe dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *describe = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.contentLabel.text = [describe componentsJoinedByString:@"\n"];
    
    self.bottomLabel.text = [NSString stringWithFormat:@"做工:%@ 颜色:%@", model.GoodsGrade, model.GoldColor];
    self.productionTimeLabel.text = model.GoodsMadeCircle;
    self.bottomLabel.hidden = model.GoodsType != 2;
    self.productionTimeLabel.hidden = model.GoodsType != 2;
    
    NSString *priceStr = isFromDianpu ? model.PlusShopGoodsPrice : model.GoodsPrice;
    NSString *prefixStr = model.GoodsValuationPriceTypeText ? : @"";
    NSString *subfixStr = [model.PriceDiffRang isEqualToString:@"±0%"] ? @"" : model.PriceDiffRang;
    subfixStr = subfixStr ? : @"";
    NSString *price = [NSString stringWithFormat:@"￥%@", priceStr];
    NSString *str = [NSString stringWithFormat:@"%@%@%@", prefixStr, price, subfixStr];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : UIColorHex(FF1F1F)} range:[str rangeOfString:price]];
    self.priceLabel.attributedText = attr;
    
    [self.locationBtn setTitle:model.GoodsLocation forState:UIControlStateNormal];
    
    // 阴影
    [self setShadowWithHasShadow:hasShadow isGray:isGray];
    
    self.selloutImageView.hidden = model.SysStatus.intValue != 9;
    
}

- (void)setShadowWithHasShadow:(BOOL)hasShadow isGray:(BOOL)isGray
{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH - 20, self.model.h - 10);
    self.contentView.layer.cornerRadius = 10;
    if (hasShadow) {
        // 阴影
        self.contentView.backgroundColor = UIColorHex(#F5F5F5);
//        self.contentView.layer.shadowColor = kMainColor.CGColor;
//        self.contentView.layer.shadowOffset = CGSizeZero;
//        self.contentView.layer.shadowOpacity = 0.2;
//        self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5].CGPath;
    } else {
        if (isGray) {
            // 灰色背景 UIColorHex(FBFBFB);
            self.contentView.backgroundColor = UIColorHex(FBFBFB);
            
            UIBezierPath *bPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5];
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.strokeColor = UIColorHex(DEDEDE).CGColor;
            layer.lineWidth = 1;
            layer.lineCap = kCALineCapRound;
            layer.lineDashPattern = @[@5, @5];
            layer.path = bPath.CGPath;
            layer.fillColor = [UIColor clearColor].CGColor;
            [self.contentView.layer insertSublayer:layer atIndex:0];
            
        } else {
            self.contentView.backgroundColor = [UIColor clearColor];
        }
    }
}

- (void)setupSubviews
{

    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.layer.cornerRadius = 10;
    _leftImageView.layer.masksToBounds = YES;
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self.contentView).mas_offset(-12);
        make.width.height.mas_equalTo(65 * kScale);
    }];
    
    _selloutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopcart_sellout"]];
    _selloutImageView.hidden = YES;
    _selloutImageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_selloutImageView];
    [_selloutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.leftImageView);
    }];
    
    _leftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
    [self.contentView addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.leftImageView);
    }];
    
    _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:15];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.equalTo(self.leftImageView.mas_right).mas_offset(10);
    }];
    
    //    SCREEN_WIDTH - 10 - 10 - 65 * kScale - 10 - 10 
    _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(5);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    _locationBtn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
    [_locationBtn setTitleColor:UIColorHex(DEDEDE) forState:UIControlStateNormal];
    [_locationBtn setImage:[UIImage imageNamed:@"shopcart_dizhi"] forState:UIControlStateNormal];
    _locationBtn.titleLabel.font = [UIFont systemFontOfSize:9];
//    _locationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.contentView addSubview:_locationBtn];
    [_locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];
    
    // 生产周期
    _productionTimeLabel = [UILabel labelWithTitle:@"" color:kMainColor fontSize:12];
    [self.contentView addSubview:_productionTimeLabel];
    [_productionTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).mas_offset(5);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    // 价格
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(DEDEDE) fontSize:9];
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.productionTimeLabel.mas_bottom).mas_offset(5);
        make.right.equalTo(self.productionTimeLabel);
    }];
    
    // 做工、颜色
    _bottomLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:11];
    [self.contentView addSubview:_bottomLabel];
    [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.priceLabel);
        make.left.equalTo(self.titleLabel);
        make.right.lessThanOrEqualTo(self.priceLabel.mas_left).mas_offset(-5);
    }];
    
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.x += 5;
    frame.size.width -= 10;

    frame.origin.y += 5;
    frame.size.height -= 10;

    [super setFrame:frame];
}

@end
